********************************************************************************
** 	TITLE:		US84_20_results                                               ** 	
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       us1976_2020                                                   **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* Import the dataset

import excel us1976_2020.xlsx, first clear

keep if year > 1980

* Generate party variable

gen party = 1 if party_name == "DEMOCRAT"
replace party = 2 if party_name == "REPUBLICAN"

* Generate rank of parties

gsort year state -candidatevotes
bysort year state : gen rank = _n

* Generate margins of victory

gen share = (candidatevotes/totalvotes)*100

bysort year state : gen margin = share[1] - share[2]

* Labelling parties

label define party 1 "Democratic Party" 2 "Republican Party"
label values party party

* Drop duplicates

duplicates tag year state, gen(dup)
duplicates drop year state, force
drop dup

* Saving file

save US84_20_results, replace